﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class TypeBinding : SyntaxNode
{
    private readonly SyntaxToken _asKeywordToken;


    private QualifiedName _qualifiedName;

    internal TypeBinding(SyntaxNode? parent, SyntaxToken asKeywordToken) :
        base(SyntaxKinds.TypeBinding, parent)
    {
        _qualifiedName = QualifiedName.Default;
        SetChildField(ref _asKeywordToken, asKeywordToken);
    }

    /// <summary>
    ///     空类型绑定。
    /// </summary>
    public static TypeBinding Void { get; } = new(null, SyntaxToken.Default);

    /// <summary>
    ///     As 关键字
    /// </summary>
    public SyntaxToken AsKeywordToken => _asKeywordToken;

    /// <summary>
    ///     绑定的类型全名
    /// </summary>
    public QualifiedName FullQualifiedName => _qualifiedName;


    /// <summary>
    ///     绑定类型的数组维度数
    /// </summary>
    public int Rank { get; set; }


    public static bool IsVoid(TypeBinding typeBinding)
    {
        return ReferenceEquals(typeBinding, Void);
    }


    public TypeBinding Update(QualifiedName qualifiedName)
    {
        SetChildField(ref _qualifiedName, QualifiedName.IsDefault, qualifiedName);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, AsKeywordToken, FullQualifiedName);
        base.AcceptVisitor(visitor);
    }
}


public static partial class NodeFactory
{
    public static TypeBinding CreateTypeBinding(SyntaxNode? parent, SyntaxToken asKeywordToken,
        QualifiedName fullQualifiedName)
    {
        Debug.Assert(asKeywordToken.IsNode(SyntaxKinds.AsKeywordToken));
        TypeBinding binding = new(parent, asKeywordToken);
        return binding.Update(fullQualifiedName);
    }
}